clear all
set more off
cap log close

********************************************************************************
***** Project: The Short and Long Term Effects of In-Person Performance Feedback
********************************************************************************
***** A. R. Soetevent & G. J. Romensen
********************************************************************************
***** Analysis Match Effects Coaching - Various Heterogeneity Effects
********************************************************************************
*global filepath "C:\JPEMicReplication"
*global paperpath "$filepath\TablesGraphs"

log using "$filepath/Logs/X07HetEffectsCoaching.log", replace

/*** Notes ***/
 

use "$filepath\DEPO\DataMainAnalysisDEPO.dta"



* merge with status (Coaching/Driving) of Eco-coaches on a given day, matched by date
merge m:1 datum using "$filepath\DEPO\coachDrivingAndCoachingDatesDEPO.dta"

* looks good
drop _merge

******************************************************************************************
** A. Determine whether a driver's coach is also working on this day
******************************************************************************************
local eco_nr "939 1404 519 1286 1610 531 594 731 808 936 1297 1402 1618"
** A.1. Coach of driver is present at DATE driver is driving
gen byte CoachPresentAtDate = 0 
foreach x of local eco_nr {
	replace CoachPresentAtDate = 1 if (coach`x'Drives == 1 | coach`x'Coaches == 1) & eco_coach_nr_rug == `x'
}

replace CoachPresentAtDate = 1 if (coach1618Drives == 1 | coach1618Coaches == 1) & eco_coach_nr_rug == 808
label variable CoachPresentAtDate "Coach of this driver is scheduled to drive or coach this day"

** A.2. Coach of driver is present at TRIP driver is driving: the necessary and sufficient conditions for this are that the shift of the coach starts BEFORE the end of the driver's trip and that the driver's trip starts BEFORE the end of the coach's shift. Because coaches do not check in at the dates at which they coach (no start and end of shift recorded), we assume that at those days, the coach is present all day.

gen byte CoachPresentAtShift = 0 
foreach x of local eco_nr {
	replace CoachPresentAtShift = 1 if  eco_coach_nr_rug == `x' &  (coach`x'Coaches == 1 |  (coach`x'startshift < werkelijke_eindtijd_rit & coach`x'endshift > werkelijke_begintijd_rit))
}


replace CoachPresentAtShift = 1 if  eco_coach_nr_rug == 808 & (coach1618Coaches == 1 | (coach1618startshift < werkelijke_eindtijd_rit & coach1618endshift > werkelijke_begintijd_rit))


label variable CoachPresentAtShift "Shift coach of this driver overlaps with trip driver"


** Determine TIME that driver receiving coaching (to see whether drivers benefit from receiving coaching early on the day)
bysort chauf_nr_rug : egen coachtimehulp = min(werkelijke_begintijd_rit)  if datum == coachdatum_1
format  coachtimehulp %tc
by chauf_nr_rug: egen coachtime = max(coachtimehulp)
format  coachtime %tc
drop coachtimehulp
label variable coachtime "time start shift driver at day of coaching"
gen hourstartshiftcoachDate = hh(coachtime)
hist hourstartshiftcoachDate
label variable hourstartshiftcoachDate "hour start shift driver at day of coaching"


hist tijdonderweg, name(histcumulativehours, replace)
tabstat tijdonderweg, s(mean sd min max p50 n)
sum  tijdonderweg, detail
global medianhours = r(p50)

gen byte Fatigue = 0 
replace Fatigue = 1 if tijdonderweg >= $medianhours
label variable Fatigue "= 1 if at start trip driver is already working longer than the median shift duration at start trip"


******************************************************************************************
** B. Various Heterogeneous Treatment Effects 
******************************************************************************************
rename bustype bustypes
** Drop all Irisbus observations 
drop if bustypes==3
gen byte ZH = 0 
replace ZH = 1 if regio == "ZH"

** Analysis: based on Treatment region
drop if regio == "ZH"

replace intouro = 0 if intouro ==.

* Drop months with imperfect tracking by coaches 
drop if datum>date("30-4-2016", "DMY")

* Drop eco-coaches [chauf_nr_rug: randomly generated depository numbers!]
local eco_nr "939 1404 519 1286 1610 531"

foreach x of local eco_nr {
drop if chauf_nr_rug==`x'
}

** Newly defined variables [geplande_ritafstand is in meters, for this reason *1000]
gen aantal_haltespKM = (1000*aantal_haltes)/geplande_ritafstand
label variable aantal_haltespKM "Nr. stops per km."

/*** Determine the set of observations used for the analysis ***/
gen byte regobsfuel=1
replace regobsfuel=0 if geplande_ritafstand==. | lnovcheckins==. | punctuality==. | aantal_haltes==. | dep_fueleconomyLpKM==.

gen byte regobsabc=1
replace regobsabc=0  if geplande_ritafstand==. | lnovcheckins==. | punctuality==. | aantal_haltes==. | dep_acceleratie==. | dep_bochten==. | dep_rem==.


***********************************
* Heterogeneity driver characteristics
***********************************

** Calculate years in service at 01-01-2016
gen dienstjaren = 2016-jaardienst
hist dienstjaren, width(1) start(0) name(histdienstjaren, replace)
gen byte dienstgroup=.
replace dienstgroup=1 if dienstjaren<8
replace dienstgroup=2 if dienstjaren>=8 & dienstjaren<16
replace dienstgroup=3 if dienstjaren>=16 & dienstjaren<30
replace dienstgroup=4 if dienstjaren>=30

label variable dienstgroup "1: <8 years; 2: 8-16 years; 3: 16-30 years; 4: >= 30 years"

gen age = 2016-gebjaar
hist age, width(1) start(18)
gen byte agegroup=.
replace agegroup=1 if age<50
replace agegroup=2 if age>=50 & age<55
replace agegroup=3 if age>=55 & age<60
replace agegroup=4 if age>=60 

label variable agegroup "1: <50 years; 2: 50-55 years; 3: 55-60 years; 4: >= 60 years"
	

****************************************
*** A. Create global list of covariates
****************************************
* outcome variables
global abcd "acceleratie rem bochten fueleconomyLpKM"

global covMaand "maand_1 maand_2 maand_3 maand_4 maand_6 maand_7 maand_8 maand_9 maand_10 maand_11 maand_12" 
* (May = default)

global covBusType "vdl10 vdl14 iris10 iris10cng iris12 iris12cng intouro"
* (vdl12 = default - more than 50 per cent of observations)

global covWeather "Temp6_3C Temp3_0C Temp0_3C Temp3_6C Temp9_12C Temp12_15C Temp15_18C Temp18_21C Temp21_24C Temp24_27C Rain5_10mm Rain10_15mm Rain15_20mm Rain20_30mm Rain30_40mm Wind0_2ms Wind4_6ms Wind6_8ms Wind8_10ms Wind10_20ms"  
*(Defaults: Wind2_4ms, Temp6_9C, Rain0_5mm)

global covEnvironm "ochtendspits avondspits uitleenrit geplande_ritafstand aantal_haltespKM stadsrit"

global covPassengers "lnovcheckins ovcheckinsmissing"

global covEndogenous "punctuality"



** Postcoaching dummies: non treatment-specific
global  covPostCoaching
	foreach x of numlist 0(7)70 {
		global covPostCoaching "$covPostCoaching daysaftercoachingA`x'" 
	}
	global covPostCoaching "$covPostCoaching daysaftercoachingA70plus"


** Precoaching dummies: non treatment-specific
global covPreCoaching
global covPreCoaching "$covPreCoaching daysbeforecoachingA70min"
	foreach x of numlist 70(7)14 {
		global covPreCoaching "$covPreCoaching daysbeforecoachingA`x'" 
	}
	
	

** SPECIFIC TO THIS ROBUSTNESS CHECK: Postcoaching dummies: specific to whether driver experiences FATIGUE or not
global  covPostCoachingFatigue
global  covPostCoachingNoFatigue


foreach x of numlist 0(7)70 {
		gen byte daysaftercoachA`x'Fatigue = daysaftercoachingA`x' * Fatigue
		gen byte daysaftercoachA`x'NoFatigue = daysaftercoachingA`x' * (1- Fatigue) 
		global covPostCoachingFatigue "$covPostCoachingFatigue daysaftercoachA`x'Fatigue" 
		global covPostCoachingNoFatigue "$covPostCoachingNoFatigue daysaftercoachA`x'NoFatigue" 
}
	
gen byte daysaftercoachA70plusFatigue = daysaftercoachingA70plus * Fatigue
gen byte daysaftercoachA70plusNoFatigue = daysaftercoachingA70plus * (1- Fatigue) 

global covPostCoachingFatigue "$covPostCoachingFatigue daysaftercoachA70plusFatigue"
global covPostCoachingNoFatigue "$covPostCoachingNoFatigue daysaftercoachA70plusNoFatigue"


** SPECIFIC TO THIS ROBUSTNESS CHECK: Precoaching dummies: specific to whether coach is present at DATE or SHIFT

global covPreCoachingFatigue
global covPreCoachingNoFatigue

gen byte daysbeforecoachA70minFatigue = daysbeforecoachingA70min * Fatigue
gen byte daysbeforecoachA70minNoFatigue =  daysbeforecoachingA70min * (1- Fatigue)

global covPreCoachingFatigue "$covPreCoachingFatigue daysbeforecoachA70minFatigue"
global covPreCoachingNoFatigue "$covPreCoachingNoFatigue daysbeforecoachA70minNoFatigue"

foreach x of numlist 70(7)14 {
		gen byte daysbeforecoachA`x'Fatigue = daysbeforecoachingA`x' * Fatigue
		gen byte daysbeforecoachA`x'NoFatigue= daysbeforecoachingA`x' * (1- Fatigue) 
						
		global covPreCoachingFatigue "$covPreCoachingFatigue daysbeforecoachA`x'Fatigue" 
		global covPreCoachingNoFatigue "$covPreCoachingNoFatigue daysbeforecoachA`x'NoFatigue" 
	}


	
set matsize 10000

xtset chauf_nr_rug 
******************************************************************************
***  [Table 5: Post-Coaching Heterogenous Treatment Effect Tests]
******************************************************************************

** REGRESSIONS **
gen byte selectie=0
local i = 0
foreach var in $abcd {
	local i = `i' + 1
		if `i'<4 {
		replace selectie=regobsabc 
		}
		if `i'==4 {
		replace selectie=regobsfuel 
		}
	if `i'==1 { 
		local ynaam  "Change in number of events per 10km" 
		local yschaal "r(-2 .2)" 
		local ylabeltje "-2 -1.5 -1 -0.5 0"
	}
	else if `i'==2 { 
			local ynaam  "Change in number of events per 10km" 
		local yschaal "r(-0.2 0.2)" 
		local ylabeltje "-0.2 -0.1 0 0.1 0.2"
	}
	else if `i'==3 { 
		local ynaam  "Change in number of events per 10km" 
		local yschaal "r(-0.2 0.2)" 
		local ylabeltje "-0.2 -0.1 -0.0 0.1 0.2"
	}
	else if `i'==4 { 
		local ynaam  "Change in fuel economy (liters/100km)" 
		local yschaal "r(-0.8 0.2)" 
		local ylabeltje "-0.8 -0.6 -0.4 -0.2 0 0.2"
	}
	
	
** A.1 Does post-coaching effect depend on whether driver is coached in morning or afternoon?
*  ANS: We cannot tell because we do not have time of coaching driver


* (2) FE + controls + day and bustype fixed effects (including interactions)

** A.1 Coaching effect and increased effort norm
xtreg dep_`var' 1.postcoaching#ibn.CoachPresentAtShift $covBusType $covPassengers $covEnvironm  i.bustypes i.datum i.bustypes#i.datum if selectie==1, fe cluster(chauf_nr_rug)
estadd loc controls "Yes", replace
estadd loc driverfe "Yes", replace
estadd loc dayfe 	"Yes", replace	
estadd loc busdayfe "Yes", replace 
estimates store cpdyesno`var'2,  title(cpdyesno`var'2)

test 1.postcoaching#0.CoachPresentAtShift = 1.postcoaching#1.CoachPresentAtShift

** A.2 Coaching effect depends on Fatigue?
xtreg dep_`var' 1.postcoaching#ibn.Fatigue  $covBusType $covPassengers $covEnvironm  i.bustypes i.datum i.bustypes#i.datum if selectie==1,  fe cluster(chauf_nr_rug)
estadd loc controls "Yes", replace
estadd loc driverfe "Yes", replace
estadd loc dayfe 	"Yes", replace	
estadd loc busdayfe "Yes", replace 
estimates store rob_fate`var'2,  title(rob_fat`var'2)

test 1.postcoaching#0.Fatigue = 1.postcoaching#1.Fatigue 

}


eststo clear

graph close
log close
